﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nwrole.plugin;
using System.Threading;
using nwrole.toolbox.sync.entity;
using nwrole.toolbox.OneWaySoapChannelLib.Channel;

namespace nwrole.toolbox.OneWaySoapChannelLib.plugin
{
    public class OneWaySoapPlugin : nwrole.plugin.PluginBase
    {

        [NwroleConfig("ConnectionString")]
        public string ConnectionString { get; set; }

        private bool runThread = true;
        private Thread thread = null;

        protected override void OnStart()
        {
            thread = new Thread(() => MyThread());
            thread.Start(); 
        }

        protected override void OnStop()
        {
            runThread = false;
            System.Threading.Thread.Sleep(3000);
            if (thread != null)
            {
                thread.Abort();
            }
        }

        protected void MyThread()
        {
            SyncEntityRepository repository = new SyncEntityRepository(ConnectionString, ConnectionString);
            MessageDispatcher dispatcher = new MessageDispatcher(repository);

            while (runThread)
            {
                Thread.Sleep(new TimeSpan(0,0,30));
                dispatcher.Send(null);
            }
            thread = null;
        }
    }
}
